import http from './http'

// 学习统计相关类型定义
export interface DailyStudyData {
  date: string
  studyTimeSeconds: number
  formattedTime: string
  sessionsCount?: number
}

export interface CourseStudyTime {
  courseId: number
  courseTitle?: string
  studyTimeSeconds: number
  formattedTime: string
  progressPercentage?: number
}

export interface StudyTypeStats {
  sessionType: string
  sessionCount: number
  studyTimeSeconds: number
  formattedTime: string
  percentage?: number
}

export interface HourlyStudyData {
  hour: number
  sessionCount: number
  studyTimeSeconds: number
  formattedTime: string
}

export interface ProgressStats {
  totalLessonsEnrolled: number
  lessonsCompleted: number
  overallProgressPercentage: number
  coursesCompleted: number
  examsCompleted: number
  certificatesEarned: number
}

export interface LearningStats {
  totalStudyTimeSeconds: number
  formattedStudyTime: string
  studyDaysCount: number
  continuousStudyDays: number
  coursesStudied: number
  notesCreated: number
  dailyStudyData: DailyStudyData[]
  courseRanking: CourseStudyTime[]
  sessionTypeStats: Record<string, StudyTypeStats>
  hourlyData: HourlyStudyData[]
  progressStats: ProgressStats
}

export interface LearningSession {
  id: number
  userId: number
  courseId: number
  lessonId?: number
  startTime: string
  endTime?: string
  durationSeconds: number
  progressPercentage: number
  sessionType: 'VIDEO' | 'DOCUMENT' | 'EXAM' | 'READING'
  createdAt: string
  updatedAt: string
}

// 学习分析API
export const analyticsApi = {
  // 获取学习统计数据
  getLearningStats(days = 30): Promise<LearningStats> {
    return http.get('/v1/learning-analytics/stats', {
      params: { days }
    })
  },

  // 开始学习会话
  startLearningSession(
    courseId: number, 
    lessonId?: number, 
    sessionType: 'VIDEO' | 'DOCUMENT' | 'EXAM' | 'READING' = 'VIDEO'
  ): Promise<LearningSession> {
    return http.post('/v1/learning-analytics/sessions/start', null, {
      params: { courseId, lessonId, sessionType }
    })
  },

  // 结束学习会话
  endLearningSession(sessionId: number, progressPercentage?: number): Promise<void> {
    return http.put(`/v1/learning-analytics/sessions/${sessionId}/end`, null, {
      params: progressPercentage ? { progressPercentage } : {}
    })
  },

  // 更新学习进度
  updateLearningProgress(sessionId: number, progressPercentage: number): Promise<void> {
    return http.put(`/v1/learning-analytics/sessions/${sessionId}/progress`, null, {
      params: { progressPercentage }
    })
  },

  // 获取当前活跃的学习会话
  getActiveSession(): Promise<LearningSession | null> {
    return http.get('/v1/learning-analytics/sessions/active')
  }
}

